import java.awt.*;
import javax.swing.event.DocumentEvent;
import javax.swing.text.*; 
public class RobocodeView extends PlainView
{ 

   public RobocodeView(Element elem)
   {
       super(elem);
   } 

   public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)
   {
       super.changedUpdate(e, a, f);
       JavaDocument d = (JavaDocument)e.getDocument();
       if(d.isNeedsRedraw())
       {
           getContainer().repaint();
           d.setNeedsRedraw(false);
       }
   } 

   protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
       throws BadLocationException
   {
       Document doc = getDocument();
       Segment segment = new Segment();
       Segment token = getLineBuffer();
       doc.getText(p0, p1 - p0, segment);
       int count = p1 - p0;
       int left = 0;
       int state = 0;
       int elementIndex = doc.getDefaultRootElement().getElementIndex(p0);
       AttributeSet lineAttributes = doc.getDefaultRootElement().getElement(elementIndex).getAttributes();
       if(lineAttributes.isDefined("inComment"))
           state = 4;
       for(int i = 0; i < count; i++)
       {
           if(state == 0)//text
           {
               if(Character.isLetter(segment.array[i + segment.offset]) && Character.isLowerCase(segment.array[i + segment.offset]))
               {
                   g.setColor(textColor);
                   doc.getText(p0 + left, i - left, token);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i;
                   state = 1;//keywords
               } else
               if(segment.array[i + segment.offset] == '/')
               {
                   g.setColor(textColor);
                   doc.getText(p0 + left, i - left, token);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i;
                   state = 2;//comment
               } else
               if(segment.array[i + segment.offset] == '"')
               {
                   g.setColor(textColor);
                   doc.getText(p0 + left, i - left, token);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i;
                   state = 3;//string
               }else
               if(segment.array[i + segment.offset] == '\'')
               {
                   g.setColor(textColor);
                   doc.getText(p0 + left, i - left, token);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i;
                   state = 5;//charactor
               }
               continue;
           }
           if(state == 1)//keywords
           {
               if(!Character.isLetter(segment.array[i + segment.offset]))
               {
                   doc.getText(p0 + left, i - left, token);
                   if(Keywords.isKeyword(token))
                       g.setColor(keywordColor);
                   else
                       g.setColor(textColor);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i;
                   state = 0;
                   if(segment.array[i + segment.offset] == '/')
                       state = 2;
                   else
                   if(segment.array[i + segment.offset] == '"')
                       state = 3;
               }
               continue;
           }
           if(state == 2)
           {
               if(segment.array[i + segment.offset] == '/')
                   break;
               if(segment.array[i + segment.offset] == '*')
                   state = 4;
               else
                   state = 0;
           } else
           if(state == 4)
           {
               if(i > 0 && segment.array[i + segment.offset] == '/' && segment.array[(i + segment.offset) - 1] == '*')
               {
                   doc.getText(p0 + left, (i + 1) - left, token);
                   g.setColor(commentColor);
                   x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
                   left = i + 1;
                   state = 0;
               }
           } else
           if(state == 3 && segment.array[i + segment.offset] == '"')
           {
               doc.getText(p0 + left, (i + 1) - left, token);
               g.setColor(stringColor);
               x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
               left = i + 1;
               state = 0;
           }
		   if(state == 5 && segment.array[i + segment.offset] == '\'')
           {
               doc.getText(p0 + left, (i + 1) - left, token);
               g.setColor(stringColor);
               x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
               left = i + 1;
               state = 0;
           }
       } 

       doc.getText(p0 + left, p1 - p0 - left, token);
       if(state == 1)
       {
           if(Keywords.isKeyword(token))
               g.setColor(keywordColor);
           else
               g.setColor(textColor);
       } else
       if(state == 3)
           g.setColor(stringColor);
       else
       if(state == 2 && p1 - p0 - left > 1)
           g.setColor(commentColor);
       else
       if(state == 4)
           g.setColor(commentColor);
       else
 	   if(state == 5)
           g.setColor(charactorColor);
       else
           g.setColor(textColor);
       x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
       return x;
   } 

   protected int getTabSize()
   {
       return 4;
   } 

   public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f)
   {
       super.insertUpdate(e, a, f);
       JavaDocument d = (JavaDocument)e.getDocument();
       if(d.isNeedsRedraw())
       {
           getContainer().repaint();
           d.setNeedsRedraw(false);
       }
   } 

   public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f)
   {
       super.removeUpdate(e, a, f);
       JavaDocument d = (JavaDocument)e.getDocument();
       if(d.isNeedsRedraw())
       {
           getContainer().repaint();
           d.setNeedsRedraw(false);
       }
   } 

   public static final Color commentColor = new Color(0, 128, 0);
   public static final Color stringColor = new Color(255, 0, 0);
   public static final Color keywordColor = new Color(0, 0, 150);
   public static final Color charactorColor = Color.pink;
   public static final Color textColor;
   public static final int TEXT = 0;
   public static final int KEYWORD = 1;
   public static final int COMMENT = 2;
   public static final int STRING = 3;
   public static final int MULTILINECOMMENT = 4;
   public static final int CHARACTOR = 5;

   static
   {
       textColor = Color.black;
   }
} 

